function ajax(url) {
    return new Promise(function(resolve, reject) {
        // foo() 可以捕获到异常
        var xhr = new XMLHttpRequest()
        xhr.open('GET',url)
        xhr.responseType = 'json'
        xhr.onload = function() {
            if(this.status === 200) {
                resolve(this.response)
            }else {
                reject(new Error(this.statusText))
            }
        }
        xhr.send()
    }) 
}
// 构造器
async function main () {
    try {
        const users = await ajax('/api/users.json')
        console.log(users)
        const posts = await ajax('api/posts/json')
        console.log(posts)
    }catch(e) {
        console.log(e)
    }
}
const promise = main()
promise.then(()=> {
    console.log("all conpleted")
})